//
//  MCPaySelectedLotteryPickView.m
//  TLYL
//
//  Created by MC on 2017/10/13.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCPaySelectedLotteryPickView.h"
#import "MCDataTool.h"

#define HEIGHT  262

@implementation PickerRow
singleton_m(PickerRow)




@end


@interface MCPaySelectedLotteryPickView()<UIPickerViewDelegate,UIPickerViewDataSource>
/** view */
@property (nonatomic,strong) UIView *topView;

/** srting */
@property (nonatomic,strong) NSString *selectedQuestion;
@property (nonatomic,strong) MCBankModel * selectedBankModel;
@property (nonatomic,strong) MCShowInfoModel * selectedShowInfoModel;
@property (nonatomic,strong) MCPaymentNewModel * paymentNewModel;


@end

@implementation MCPaySelectedLotteryPickView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:CGRectMake(0, 0, G_SCREENWIDTH, HEIGHT+G_SCREENHEIGHT)];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    UIView *backView = [[UIView alloc]init];
    [self addSubview:backView];
    backView.backgroundColor=RGB(0, 0, 0);
    backView.alpha=0.5;
    backView.frame=CGRectMake(0, 0, G_SCREENWIDTH,HEIGHT+G_SCREENHEIGHT);
    
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, G_SCREENHEIGHT, G_SCREENWIDTH, HEIGHT)];
    self.topView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.topView];
    
    CGFloat H =40;
    UIView * upView=[[UIView alloc]init];
    [self.topView addSubview:upView];
    upView.backgroundColor=RGB(248, 248, 248);
    upView.frame=CGRectMake(0, 0, G_SCREENWIDTH, H);
    UIButton * doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [doneBtn setTitle:@"确定" forState:UIControlStateNormal];
    [doneBtn setTitleColor:RGB(51, 142, 251) forState:UIControlStateNormal];
    [doneBtn setFrame:CGRectMake(G_SCREENWIDTH-110, 0, 100, H)];
    [doneBtn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [upView addSubview:doneBtn];
    doneBtn.tag=8001;
    doneBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    doneBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    
    UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:RGB(51, 142, 251) forState:UIControlStateNormal];
    [cancelBtn setFrame:CGRectMake(10, 0, 100, H)];
    [cancelBtn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [upView addSubview:cancelBtn];
    cancelBtn.tag=8002;
    cancelBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    cancelBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    
    UILabel *titlelb = [[UILabel alloc]initWithFrame:CGRectMake(100, 0, G_SCREENWIDTH-200, H)];
    titlelb.backgroundColor = [UIColor clearColor];
    titlelb.textAlignment = NSTextAlignmentCenter;
    titlelb.text = @"请选择开奖次数";
    titlelb.font = [UIFont systemFontOfSize:15];
    [self.topView addSubview:titlelb];
    titlelb.textColor=RGB(46, 46, 46);
    if (_title.length>0) {
        titlelb.text = _title;
    }
    self.pickerView = [[UIPickerView alloc]init];
    [self.pickerView setFrame:CGRectMake(0, H, G_SCREENWIDTH, HEIGHT-H)];
    [self.pickerView setBackgroundColor:RGB(255, 255, 255)];
    [self.pickerView setDelegate:self];
    [self.pickerView setDataSource:self];
    [self.pickerView selectRow:0 inComponent:0 animated:YES];
    [self.topView addSubview:self.pickerView];
    
    
}

#pragma mark-快速创建
+(instancetype)pickerView{
    return [[self alloc]init];
}

#pragma mark-弹出
- (void)show
{
    [self showInView:[UIApplication sharedApplication].keyWindow];
    //    [self.pickerView selectRow:1 inComponent:0 animated:YES];
    //    [self.pickerView reloadAllComponents];
}

#pragma mark-添加弹出移除的动画效果
- (void)showInView:(UIView *)view
{
    // 浮现
    [UIView animateWithDuration:0.5 animations:^{
        CGPoint point = self.center;
        point.y -= HEIGHT;
        self.center = point;
    } completion:^(BOOL finished) {
        if (self.selectedPickerBlock) {
            self.selectedPickerBlock();
        }
    }];
    [view addSubview:self];
}


-(void)btnClickAction:(UIButton *)btn{
    if (btn.tag==8001){
        if (!self.selectedQuestion) {
            self.selectedQuestion = self.dataSource[0];
        }
        if (_Type==MCPaySelectedPickViewType_FastPay) {
            if (!self.selectedBankModel) {
                self.selectedBankModel = self.dataSource[0];
            }
        }
        if (_Type==MCPaySelectedPickViewType_MCShowInfoModel) {
            if (!self.selectedShowInfoModel) {
                self.selectedShowInfoModel = self.dataSource[0];
            }
        }
        if (_Type==MCPaySelectedPickViewType_RechargeType){
            if (!self.paymentNewModel) {
                self.paymentNewModel = self.dataSource[0];
            }
        }
        
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
        CGPoint point = self.center;
        point.y += HEIGHT;
        self.center = point;
    } completion:^(BOOL finished) {
#pragma mark-点击确定
        if (btn.tag==8001) {
            
            if (self.getCountBlock) {
                self.getCountBlock(self.selectedQuestion);
            }
            if (_Type==MCPaySelectedPickViewType_FastPay) {
                if (self.getBankBlock) {
                    self.getBankBlock(self.selectedBankModel);
                }
            }
            if (_Type==MCPaySelectedPickViewType_MCShowInfoModel) {
                if (self.getShowInfoModel) {
                    self.getShowInfoModel(self.selectedShowInfoModel);
                }
            }
            if (_Type==MCPaySelectedPickViewType_RechargeType){
                if (self.PaymentModelBlock) {
                    self.PaymentModelBlock(self.paymentNewModel);
                }
            }
            
            //            NSLog(@"selectedQuestion==%@",self.selectedQuestion);
        }
        [self removeFromSuperview];
    }];
    
}


// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component.
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.dataSource count];
}

#pragma mark - 代理
// 返回第component列第row行的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (_Type==MCPaySelectedPickViewType_FastPay) {
        
        MCBankModel * model = self.dataSource[row];
        return model.bankName;
        
    }else if (_Type==MCPaySelectedPickViewType_MCShowInfoModel){
        
         MCShowInfoModel * selectedShowInfoModel = self.dataSource[row];
        return selectedShowInfoModel.ThirdpartyText;
       
    }else if(_Type==MCPaySelectedPickViewType_RechargeType){
        MCPaymentNewModel * model = self.dataSource[row];
        return model.PayName;
    }else{
        return self.dataSource[row];
    }
}

// 选中第component第row的时候调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (_Type==MCPaySelectedPickViewType_FastPay) {
        self.selectedBankModel = self.dataSource[row];
    }else if (_Type==MCPaySelectedPickViewType_MCShowInfoModel){
        self.selectedShowInfoModel= self.dataSource[row];
    }else if(_Type==MCPaySelectedPickViewType_RechargeType){
        self.paymentNewModel = self.dataSource[row];
    }else{
        self.selectedQuestion = self.dataSource[row];
    }
    PickerRow * P = [PickerRow sharedPickerRow];
    P.pickerRow = row;
}

@end










